import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
// 公共布局组件
/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

// 动态路由 => 后期做后系统页面访问控制=>路由规则的动态添加(根据当前登录人的权限标识) => 权限控制
// 引入多个动态路由模块
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'
/**
 * 配置路由:
 * 1. 父路由使用Layout作为component的值
 * 2. 真正配置的页面放到children里配置
 * 3. 加上meta里面配置title和icon来自动显示到左侧导航菜单里
 * 说明: 通过 hidden: true (放在父路由) 控制路由不在菜单中显示
 */
export const asyncRoutes = [
  // 把路由配置已经抽离成单独的js模块了
  // 组织架构
  departmentsRouter,
  // 公司设置
  settingRouter,
  // 员工管理
  employeesRouter,
  // 权限管理
  permissionRouter,
  // 审批页面
  approvalsRouter,
  // 考勤页面
  attendancesRouter,
  // 工资
  salarysRouter,
  // 社保
  socialRouter
]

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
// 静态路由 (不做访问的限制=>任何人都可以访问)
export const constantRoutes = [
  // 登录页
  {
    path: '/login',
    // 路由的懒加载(通过一个函数返回对象实现按需加载)
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 实例页面
  // 首页
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
  // excel 导入页面
  {
    path: '/import',
    component: Layout,
    hidden: true, // 不在菜单显示
    children: [{
      path: '',
      name: 'import',
      component: () => import('@/views/import/index.vue')
    }]
  }
  // 404 page must be placed at the end !!!
  // 必须放在最后
  // { path: '*', redirect: '/404', hidden: true }
  // 动态路由
]

// 使用工厂模式，每次调用都会得到一个新的路由实例 => 复用性强
const createRouter = () => new Router({
  // 默认的路由模式是 hash 哈希模式
  mode: 'history', // require service support
  // 每次页面切换都会把y轴重置为0 => 为了防止切换组件以后位置继承了上一个页面的滚动位置 => 因为他们共用的 layout 公共布局组件
  scrollBehavior: () => ({ y: 0 }),
  // 数组的合并 => [...arr1, ...arr2]
  // routes: [...constantRoutes, ...asyncRoutes]
  // 只留下静态路由 => 根据权限点数据动态添加路由
  routes: [...constantRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// ----------------------- 核心方法 -------------------
// 重置路由，防止换个登录人之后权限没有改变(相对比较复杂) => 避免路由规则缓存
// eslint-disable-next-line space-before-function-paren
export function resetRouter () {
  // 调用工厂函数重新new路由
  const newRouter = createRouter()
  // 清除路由和映射表的关系
  router.matcher = newRouter.matcher // reset router
}

export default router
